package com.le.tester.leetcode;

import java.util.Arrays;

/**
 * createTime：2024/4/19 14:46
 * description：下一个排列
 */
public class PermutationNext {

    public void nextPermutation(int[] nums) {

        int length = nums.length;

        for (int i = length - 1; i > 0; i--) {
            if (nums[i] > nums[i - 1]) {
                Arrays.sort(nums, i, length);
                for (int j = i; j < length; j++) {
                    if (nums[j] > nums[i - 1]) {
                        int temp = nums[j];
                        nums[j] = nums[i - 1];
                        nums[i - 1] = temp;
                        return;
                    }
                }
            }
        }
        Arrays.sort(nums);
    }

    public static void main(String[] args) {
        int[] ints = {3, 2, 1};
        new PermutationNext().nextPermutation(ints);
    }
}
